package router

import (
	platformController "gitee.com/wangshh_123/cross-pay-go/src/controller/platform"
	"gitee.com/wangshh_123/cross-pay-go/src/database"
	"gitee.com/wangshh_123/cross-pay-go/src/middleware"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/hlPay"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/photonpay"

	"github.com/gin-gonic/gin"
)

// SetupPhotonPayRouter 设置光子易路由
func SetupPhotonPayRouter(api *gin.RouterGroup, photonPayClient *photonpay.PhotonPayClient, hlpayClient *hlPay.HlPayClient) {
	// 创建光子易控制器实例
	photonPayController := platformController.NewPhotonPayController(photonPayClient, hlpayClient)

	// 创建统一账户控制器实例
	unifiedAccountController := platformController.NewUnifiedAccountController(photonPayClient, hlpayClient)

	// 创建统一卡片控制器实例
	unifiedCardController := platformController.NewUnifiedCardController(photonPayClient, hlpayClient)

	// 创建统一的卡BIN控制器实例
	cardBinController := platformController.NewCardBinController()

	// 创建统一的卡列表控制器实例
	cardListController := platformController.NewCardListController(photonPayClient, hlpayClient, database.DB)

	// 创建统一的CVV控制器实例
	cardCVVController := platformController.NewCardCVVController()

	// 创建统一的卡详情控制器实例
	cardDetailController := platformController.NewCardDetailController()

	// 创建统一的持卡人控制器实例
	cardholderController := platformController.NewCardholderController()

	// 创建统一的卡历史控制器实例
	cardHistoryController := platformController.NewCardHistoryController()

	// 光子易路由组
	photonPay := api.Group("/photonpay")
	photonPay.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
	{
		// 卡片相关路由
		cards := photonPay.Group("/cards")
		cards.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
		{
			// 卡片查询相关
			cards.GET("/bin", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), cardBinController.GetCardBin)          // 基础卡BIN查询 - 根据用户平台自动选择
			cards.GET("/list", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), cardListController.GetCardList)       // 基础卡列表查询 - 根据用户平台自动选择
			cards.GET("/detail", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), cardDetailController.GetCardDetail) // 获取卡片详情 - 根据用户平台自动选择
			cards.GET("/cvv", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), cardCVVController.GetCardCVV)          // 获取卡片CVV - 根据用户平台自动选择

			// 卡片操作相关 - 使用统一控制器
			cards.POST("/open", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.OpenCard)                       // 基础开卡 - 根据用户平台自动选择
			cards.POST("/batch-open", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.BatchOpenCard)            // 批量开卡 - 根据用户平台自动选择
			cards.POST("/batch-status", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.GetBatchOpenCardStatus) // 查询批量开卡状态
			cards.POST("/update", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.UpdateCard)                   // 基础更新卡片 - 根据用户平台自动选择
			cards.POST("/freeze", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.FreezeCard)                   // 基础冻结卡片 - 根据用户平台自动选择
			cards.POST("/cancel", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.CancelCard)                   // 基础注销卡片 - 根据用户平台自动选择

			// 卡片充值相关
			cards.POST("/recharge-return", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.RechargeReturn) // 基础金额退还 - 根据用户平台自动选择
			cards.GET("/pre-recharge", photonPayController.PreRecharge)                                                                            // 换汇询价
			cards.POST("/recharge", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.RechargeCard)          // 基础卡片充值 - 根据用户平台自动选择
			cards.POST("/withdraw", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedCardController.WithdrawCard)          // 统一提现 - 使用统一接口
		}

		// 用卡人相关路由 - 使用统一的持卡人控制器
		cardholders := photonPay.Group("/cardholders")
		cardholders.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
		{
			cardholders.GET("/list", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), cardholderController.GetCardholderList)              // 基础持卡人列表查询 - 根据用户平台自动选择
			cardholders.POST("/add", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), cardholderController.CreateCardholder)               // 基础创建持卡人 - 根据用户平台自动选择
			cardholders.PUT("/edit/:cardholderId", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), cardholderController.UpdateCardholder) // 基础更新持卡人 - 根据用户平台自动选择
		}

		// 账户相关路由
		accounts := photonPay.Group("/accounts")
		accounts.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
		{
			accounts.GET("/list", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), unifiedAccountController.GetAccountList)                                                   // 基础账户列表查询 - 根据用户平台自动选择
			accounts.GET("/balance", photonPayController.GetAccountBalance)                                                                                                                    // 获取账户余额
			accounts.GET("/history", photonPayController.GetAccountHistoryList)                                                                                                                // 获取账户历史
			accounts.GET("/list/by-matrix", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), middleware.AdminMiddleware(), photonPayController.GetAccountListByMatrixAccount) // 基础账户列表查询 - 根据指定矩阵账户平台自动选择（需要管理员权限）
		}

		// Matrix账户相关路由
		matrix := photonPay.Group("/matrix")
		matrix.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
		{
			matrix.GET("/list", photonPayController.GetMatrixAccountList)   // 获取Matrix账户列表
			matrix.POST("/create", photonPayController.CreateMatrixAccount) // 创建Matrix账户
			matrix.POST("/edit", photonPayController.EditMatrixAccount)     // 编辑Matrix账户
		}

		// 资金相关路由
		funds := photonPay.Group("/funds")
		funds.Use(middleware.AuthMiddleware(), middleware.MatrixAccountMiddleware())
		{
			funds.GET("/transfer-list", photonPayController.GetFundTransferList)                                                                      // 获取资金调拨明细
			funds.POST("/matrix-transfer", photonPayController.MatrixTransfer)                                                                        // 矩阵转账
			funds.GET("/issuing-history", middleware.AuthMiddleware(), middleware.PlatformInfoMiddleware(), cardHistoryController.GetCardHistoryList) // 基础卡历史查询 - 根据用户平台自动选择
			funds.GET("/recharge-detail", photonPayController.GetRechargeCardFundsDetailList)                                                         // 获取充值卡资金明细
			funds.GET("/trade-orders", photonPayController.GetVccTradeOrderList)                                                                      // 获取VCC交易订单列表
			funds.POST("/simulate-transaction", photonPayController.SimulateTransaction)                                                              // 模拟交易
		}
	}
}
